Telegram Group & Telegram Channel
🛠 How to: как соблюдать принцип DRY

DRY (Don’t Repeat Yourself) — принцип, согласно которому каждый фрагмент знания должен существовать в системе только в одном месте. Никакого копипаста и дублирующей логики.

Проблема: дублирование валидации
// Пример плохого кода
if (user.Age < 18)
throw new Exception("User must be at least 18");

...

if (user.Age < 18)
return BadRequest("User must be at least 18");

При изменении правила — нужно помнить обновить его везде. Ловушка копипаста.

Решение: вынос логики в общее правило
public static class ValidationRules
{
public static bool IsAdult(User user) => user.Age >= 18;
}


Теперь в коде
if (!ValidationRules.IsAdult(user))
throw new Exception("User must be at least 18");

// И в другом месте:
if (!ValidationRules.IsAdult(user))
return BadRequest("User must be at least 18");


Альтернатива: использование FluentValidation или DataAnnotations
public class User
{
[Range(18, int.MaxValue, ErrorMessage = "User must be at least 18")]
public int Age { get; set; }
}


Где чаще всего нарушают DRY в .NET:


• Повторяющиеся SQL-запросы и фильтры
• Повторение одинаковых exception'ов, логов, сообщений
• Дублирование конфигурации
• UI-формы и компоненты

🐸Библиотека шарписта #буст
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/csharpproglib/5841
Create:
Last Update:

🛠 How to: как соблюдать принцип DRY

DRY (Don’t Repeat Yourself) — принцип, согласно которому каждый фрагмент знания должен существовать в системе только в одном месте. Никакого копипаста и дублирующей логики.

Проблема: дублирование валидации

// Пример плохого кода
if (user.Age < 18)
throw new Exception("User must be at least 18");

...

if (user.Age < 18)
return BadRequest("User must be at least 18");

При изменении правила — нужно помнить обновить его везде. Ловушка копипаста.

Решение: вынос логики в общее правило
public static class ValidationRules
{
public static bool IsAdult(User user) => user.Age >= 18;
}


Теперь в коде
if (!ValidationRules.IsAdult(user))
throw new Exception("User must be at least 18");

// И в другом месте:
if (!ValidationRules.IsAdult(user))
return BadRequest("User must be at least 18");


Альтернатива: использование FluentValidation или DataAnnotations
public class User
{
[Range(18, int.MaxValue, ErrorMessage = "User must be at least 18")]
public int Age { get; set; }
}


Где чаще всего нарушают DRY в .NET:


• Повторяющиеся SQL-запросы и фильтры
• Повторение одинаковых exception'ов, логов, сообщений
• Дублирование конфигурации
• UI-формы и компоненты

🐸Библиотека шарписта #буст

BY Библиотека шарписта | C#, F#, .NET, ASP.NET




Share with your friend now:
tg-me.com/csharpproglib/5841

View MORE
Open in Telegram


Библиотека шарписта | C F NET ASP NET Telegram | DID YOU KNOW?

Date: |

The SSE was the first modern stock exchange to open in China, with trading commencing in 1990. It has now grown to become the largest stock exchange in Asia and the third-largest in the world by market capitalization, which stood at RMB 50.6 trillion (US$7.8 trillion) as of September 2021. Stocks (both A-shares and B-shares), bonds, funds, and derivatives are traded on the exchange. The SEE has two trading boards, the Main Board and the Science and Technology Innovation Board, the latter more commonly known as the STAR Market. The Main Board mainly hosts large, well-established Chinese companies and lists both A-shares and B-shares.

Newly uncovered hack campaign in Telegram

The campaign, which security firm Check Point has named Rampant Kitten, comprises two main components, one for Windows and the other for Android. Rampant Kitten’s objective is to steal Telegram messages, passwords, and two-factor authentication codes sent by SMS and then also take screenshots and record sounds within earshot of an infected phone, the researchers said in a post published on Friday.

Библиотека шарписта | C F NET ASP NET from in


Telegram Библиотека шарписта | C#, F#, .NET, ASP.NET
FROM USA